package com.yint.http.bodys;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.yint.http.api.Body;
import com.yint.http.values.FileValue;
import com.yint.http.values.PartValue;

public class HttpMultiparBody implements Body
{
	private List< PartValue > params = new ArrayList< PartValue >();

	private String finalSplit = "---------------------------123821742118716";

	public HttpMultiparBody()
	{

	}

	public void addFormDataPart( PartValue part )
	{
		if ( part != null )
		{
			params.add( part );
		}
	}

	@Override
	public void toStream( OutputStream os ) throws IOException
	{
		for ( PartValue value : params )
		{
			StringBuffer strBuf = new StringBuffer();
			if ( value instanceof FileValue )
			{
				File file = ( File ) value.getValue();
				String filename = file.getName();
				String contentType = getMimeType( file.getAbsolutePath() );
				StringBuffer buf = new StringBuffer();
				strBuf.append( "\r\n" ).append( "--" ).append( finalSplit ).append( "\r\n" );
				strBuf.append( "Content-Disposition: form-data; name=\"" + value.getKey()
								+ "\"; filename=\"" + filename + "\"\r\n" );
				buf.append( "Content-Type:" + contentType + "\r\n\r\n" );
				os.write( strBuf.toString().getBytes() );
				DataInputStream in = new DataInputStream( new FileInputStream( file ) );
				int bytes = 0;
				byte[] bufferOut = new byte[ 1024 * 10 ];
				while ( ( bytes = in.read( bufferOut ) ) != -1 )
				{
					os.write( bufferOut, 0, bytes );
				}
				in.close();
			}
			else
			{
				strBuf.append( "\r\n" ).append( "--" ).append( finalSplit ).append( "\r\n" );
				strBuf.append( "Content-Disposition: form-data; name=\"" + value.getKey()
								+ "\"\r\n\r\n" );
				strBuf.append( value.getValue() );
				os.write( strBuf.toString().getBytes( "UTF-8" ) );
			}
		}

		byte[] endData = ( "\r\n--" + finalSplit + "--\r\n" ).getBytes();
		os.write( endData );
		os.flush();
		os.close();
	}

	public static String getMimeType( String filePath )
	{
		String suffix = getSuffix( new File( filePath ) );
		if ( suffix == null )
		{
			return "file/*";
		}
		FileNameMap fileNameMap = URLConnection.getFileNameMap();
		String type = fileNameMap.getContentTypeFor( filePath );
		if ( type != null && !type.isEmpty() )
		{
			return type;
		}
		return "file/*";
	}

	private static String getSuffix( File file )
	{
		if ( file == null || !file.exists() || file.isDirectory() )
		{
			return null;
		}
		String fileName = file.getName();
		if ( fileName.equals( "" ) || fileName.endsWith( "." ) )
		{
			return null;
		}
		int index = fileName.lastIndexOf( "." );
		if ( index != -1 )
		{
			return fileName.substring( index + 1 ).toLowerCase( Locale.US );
		}
		else
		{
			return null;
		}
	}

	@Override
	public String toStr() throws IOException
	{
		return null;
	}

	@Override
	public byte[] toBytes() throws IOException
	{
		return null;
	}

	@Override
	public String getHost()
	{
		return null;
	}

	@Override
	public int getPort()
	{
		return -1;
	}

	@Override
	public String getScheme()
	{
		return null;
	}

	@Override
	public String getUrl()
	{
		return null;
	}

}
